package com.example.myapplication_mutiactivity

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {
    private lateinit var editText: EditText
    private lateinit var imageView: ImageView

    private val requestCameraPermission = registerForActivityResult(
        ActivityResultContracts.RequestPermission()
    ) { isGranted: Boolean ->
        if (isGranted) {
            photoLauncher.launch(null)
        } else {
            Toast.makeText(this, "请授予相机权限以使用拍照功能", Toast.LENGTH_SHORT).show()
        }
    }

    private val photoLauncher = registerForActivityResult(
        ActivityResultContracts.TakePicturePreview()
    ) { bitmap ->
        bitmap?.let {
            imageView.setImageBitmap(it) // 显示拍摄照片
        } ?: Toast.makeText(this, "拍照取消或失败", Toast.LENGTH_SHORT).show()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        editText = findViewById(R.id.editTextText)
        imageView = findViewById(R.id.imageView)
        val btnForward = findViewById<Button>(R.id.button)
        val btnBackward = findViewById<Button>(R.id.button2)
        val btnCamera = findViewById<Button>(R.id.button3)
        val btnTimer = findViewById<Button>(R.id.button4)

        btnForward.setOnClickListener {
            val intent = Intent(this, MainActivity2::class.java)
            intent.putExtra("forward_text", editText.text.toString())
            startActivity(intent)
        }

        btnBackward.setOnClickListener {
            val intent = Intent(this, MainActivity3::class.java)
            startActivityForResult(intent, 100)
        }

        btnCamera.setOnClickListener {
            requestCameraPermission.launch(android.Manifest.permission.CAMERA)
        }

        btnTimer.setOnClickListener {
            val intent = Intent(this, MainActivity4::class.java)
            startActivity(intent)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100 && resultCode == RESULT_OK) {
            val returnText = data?.getStringExtra("backward_text") ?: ""
            editText.setText(returnText)
        }
    }
}